package com.admin.monmon.config;

import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.YearMonthDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.YearMonthSerializer;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.time.LocalDateTime;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;

/**
 * @author caixiaofeng
 * @version 1.0
 * @Description: <<>>
 * @company 捷创智能技术有限公司
 * @create 2021-04-23 16:31
 */
@Configuration
public class WebConfig {

    //全局序列化日期
    @Bean
    public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
        return builder -> {
            //序列化
            builder.deserializerByType(
                    LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))
            );
            //反序列化
            builder.serializerByType(
                    LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))
            );


            //序列化
            builder.serializerByType(
                    YearMonth.class, new YearMonthSerializer(DateTimeFormatter.ofPattern("yyyy-MM"))
            );
            //反序列化
            builder.deserializerByType(
                    YearMonth.class, new YearMonthDeserializer(DateTimeFormatter.ofPattern("yyyy-MM"))
            );
        };
    }
}
